package es.cea.agendatelefonica;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AgendaServlet extends HttpServlet {

      
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		EntityManagerFactory entityManagerFactory = (EntityManagerFactory) request.getSession().getServletContext().getAttribute("emf");
		EntityManager entityManager = entityManagerFactory.createEntityManager();
		EntityTransaction transaction = entityManager.getTransaction();
		transaction.begin();

		String jpql = "select contactoTel from ContactoTel contactoTel";
		List<ContactoTel> contactos = entityManager.createQuery(jpql).getResultList();
		
		transaction.commit();
		
		request.setAttribute("contactos", contactos);
		
		request.getRequestDispatcher("/agendaTelefonica.jsp").forward(request, response);
	}


}
